{% extends "base.html" %}
{% load navigation_tags %}
{% load authorization_tags %}
{% load display_tags %}
{% load i18n %}

{% block content %}
    {{ block.super }}
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading tight">
                    <h3 class="has-filters">
                        {{ labels.ORG_LABEL }}
                        <div class="dropdown pull-right">
                            <button id="show-filters" aria-label="Filters" data-toggle="collapse" data-target="#the-filters" class="btn btn-primary toggle-filters"> <i class="fa-solid fa-filter"></i> <i class="caret"></i> </button>
                            {% if "Product_Type_Add"|has_global_permission %}
                            <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1"
                                    data-toggle="dropdown" aria-expanded="true" aria-label="{{ labels.ORG_LABEL }} options">
                                <span class="fa-solid fa-screwdriver-wrench"></span>
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right" role="menu"
                                aria-labelledby="dropdownMenu1">
                                    <li role="presentation">
                                        <a href="{% url 'add_product_type' %}">
                                            <i class="fa-solid fa-plus"></i>
                                            {{ labels.ORG_CREATE_LABEL }}
                                        </a>
                                    </li>
                            </ul>
                            {% endif %}
                        </div>
                    </h3>
                </div>
                <div id="the-filters" class="is-filters panel-body collapse {% if ptl.form.has_changed %}in{% endif %}">
                    {% include "dojo/filter_snippet.html" with form=ptl.form %}
                </div>
            </div>
            {% if pts %}

                <div class="clearfix">
                    {% include "dojo/paging_snippet.html" with page=pts page_size=True %}
                </div>

                <div class="panel panel-default table-responsive">
                    <table id="product_types"
                           class="tablesorter-bootstrap table table-condensed table-striped">
                        <thead>
                        <tr>
                            <th></th>
                            {% comment %} The display field is translated in the function. No need to translate here as well{% endcomment %}
                            <th>{% dojo_sort request labels.ORG_LABEL 'name' 'asc' %}</th>
                            <th>{{ labels.ASSET_COUNT_LABEL }}</th>
                            <th>{% trans "Active (Verified) findings" %}</th>
                            <th class="text-center">{{ labels.ORG_CRITICAL_PRODUCT_LABEL }}</th>
                            <th class="text-center">{{ labels.ORG_KEY_PRODUCT_LABEL }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for pt in pts %}
                            <tr>
                                <td>
                                    <ul>
                                        <li class="dropdown" style="list-style:none;position:absolute">
                                            <a href="#" aria-label="Organization options menu" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true" id="dropdownMenuProductType">&nbsp;<b class="fa-solid fa-ellipsis-vertical"></b>&nbsp;</a>
                                            <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuProductType">
                                                <li role="presentation">
                                                    <a class="" href="{% url 'view_product_type' pt.id %}" role="menuitem">
                                                    <i class="fa-solid fa-rectangle-list"></i> {% trans "View" %}</a>
                                                </li>
                                                {% if pt|has_object_permission:"Product_Type_Edit" %}
                                                <li role="presentation">
                                                    <a class="" href="{% url 'edit_product_type' pt.id %}" role="menuitem">
                                                    <i class="fa-solid fa-pen-to-square"></i> {% trans "Edit" %}</a>
                                                </li>
                                                {% endif %}
                                                {% if pt|has_object_permission:"Product_Type_Add_Product" %}
                                                <li role="presentation">
                                                    <a class="" href="{% url 'add_product_to_product_type' pt.id %}" role="menuitem">
                                                        <i class="fa-solid fa-plus"></i>
                                                        {{ labels.ASSET_CREATE_LABEL }}
                                                    </a>
                                                </li>
                                                {% endif %}
                                                <li role="separator" class="divider"></li>
                                                <li role="presentation">
                                                    <a class="" href="{% url 'product_type_metrics' pt.id %}" role="menuitem">
                                                    <i class="fa-solid fa-chart-line"></i> {% trans "Metrics" %}</a>
                                                </li>
                                                <li role="presentation">
                                                    <a class="" href="{% url 'product_type_report' pt.id %}" role="menuitem">
                                                        <i class="fa-solid fa-file-lines"></i>
                                                        {{ labels.ORG_REPORT_LABEL }}
                                                    </a>
                                                </li>
                                                {% if pt|has_object_permission:"Product_Type_Delete" %}
                                                <li role="separator" class="divider"></li>
                                                <li role="presentation">
                                                    <a class="" href="{% url 'delete_product_type' pt.id %}" role="menuitem">
                                                    <i class="fa-solid fa-trash"></i> {% trans "Delete" %}</a>
                                                </li>
                                                {% endif %}
                                            </ul>
                                        </li>
                                    </ul>
                                </td>
                                <td><a href="{% url 'view_product_type' pt.id %}">{{ pt.name }}</a></td>
                                <td class="text-right"><a href="{% url 'product' %}?prod_type={{ pt.id }}"> {{ pt.prod_count }} </a></td>
                                <td class="text-right">
                                    <a href="{% url 'open_findings' %}?test__engagement__product__prod_type={{ pt.id }}"><b>{{ pt.active_findings_count }}</b></a>
                                    &nbsp;(<a href="{% url 'verified_findings' %}?test__engagement__product__prod_type={{ pt.id }}">{{ pt.active_verified_findings_count }}</a>)
                                </td>
                                <td class="text-center">
                                    {% if pt.critical_product %}
                                        <i class="text-success fa-solid fa-check"></i>
                                    {% else %}
                                        <i class="text-danger fa-solid fa-xmark"></i>
                                    {% endif %}
                                </td>
                                    <td class="text-center">
                                    {% if pt.key_product %}
                                        <i class="text-success fa-solid fa-check"></i>
                                    {% else %}
                                        <i class="text-danger fa-solid fa-xmark"></i>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
                <div class="clearfix">
                    {% include "dojo/paging_snippet.html" with page=pts page_size=True %}
                </div>
            {% else %}
                <p class="text-center">{{ labels.ORG_NONE_FOUND_MESSAGE }}</p>
            {% endif %}
        </div>
    </div>
{% endblock %}
{% block postscript %}
    {{ block.super }}
    <script>
        $(function () {
            var availableTags = [
                {% for word in name_words %}
                    "{{word}}",
                {% endfor %}
            ];
            $("#id_name").autocomplete({
                source: availableTags
            });
        });
    </script>
    {% include "dojo/filter_js_snippet.html" %}
{% endblock %}
